package org.thomias.exo4.XMLReader;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class XMLEssaiDOM {
	public static void main(String[] args) {
		try {
			String filename = "Personnage.xml";
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(new File(filename));
			System.out.println("root element ="
					+ doc.getDocumentElement().getNodeName());
			Element e = (Element) doc.getDocumentElement().getElementsByTagName("personnage").item(0);
			e = (Element) e.getElementsByTagName("age").item(0);
			System.out.println("First person age =" + e.getFirstChild().getNodeValue());
			XPathFactory xPathfactory = XPathFactory.newInstance();
			XPath xpath = xPathfactory.newXPath();
			XPathExpression expr = xpath.compile("/rootxxxx/personnage[2]/age/text()");
			String s = (String) expr.evaluate(doc, XPathConstants.STRING);

			System.out.println("s = " + s);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
